﻿using System;
using System.Web;
using System.Web.UI;
using System.Linq;
using Models;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json.Linq;
using System.Web.UI.WebControls;

public partial class Admin_store_StoreItemAdd : AdminBase
{
    public int _Id = 0,sId=0;
    public string Shopname = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.CacheControl = "no-cache";
        if (!string.IsNullOrEmpty(Request.Params["ID"]))
        {
            _Id = int.Parse(HttpContext.Current.Request.QueryString["ID"]);

        }


          sId = int.Parse(HttpContext.Current.Request.QueryString["SID"]);

        if (!Page.IsPostBack)
        {

            Button1.Attributes.Add("OnClick", "return setValue()");

            if (_Id != 0)
            {
                ShowInfo(_Id);
            }
            Entities mydb = new Entities();
            {

                Shopname = mydb.S_Store.SingleOrDefault(x => x.SID == sId).Sname;
            }
        }
    }

    private void ShowInfo(int _id)
    {

        Entities mydb = new Entities();
        {

         

            S_Store_Item _lt = mydb.S_Store_Item.First(p => p.SItemID == _id);
            txtSname.Text = _lt.Sname;

            imgArr = getImg(_lt.Simg);

            txtSintroduce.Value = _lt.Sintroduce;

            tag.Text = _lt.tag;

            people.Text = _lt.people;
            Textprice.Text = _lt.price.ToString();

            sbid.Text= _lt.sbid;
            txtDay.Text = _lt.day;
            //datetime1.value = iteamSet.out1;
            if (_lt.srvtime != null)
            { 
            JObject jobj = JObject.Parse(_lt.srvtime);

            var weekset = jobj["weekset"].Children();

            for (int i = 0; i <= 6; i++)
            {
                    string xxx = i.ToString();
                if (weekset.Contains(xxx))
                {
                        
                    ((HtmlInputCheckBox)this.Page.FindControl("week" + i)).Checked = true;

                }
            }
            var SQLweek = jobj["times"];
            week.DataSource = SQLweek;
            week.DataBind();
            }


            out1.Text = _lt.out1;
            out2.Text = _lt.out2;
            out3.Text = _lt.out3;
            out4.Text = _lt.out4;
            out5.Text = _lt.out5;
            out6.Text = _lt.out6;
            out7.Text = _lt.out7;
          

        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        Entities mydb = new Entities();
        {
            S_Store_Item Lt = new S_Store_Item();
            if (_Id != 0)
                Lt = mydb.S_Store_Item.First(p => p.SItemID == _Id);
            
            Lt.sbid = sbid.Text.Trim();

            Lt.SID = sId;
            Lt.Sname = txtSname.Text.Trim();

            Lt.Simg = xsimg.Value.Trim();//图片

            Lt.Sintroduce = txtSintroduce.Value;


            Lt.tag= tag.Text;

            Lt.people= people.Text;

            Lt.price =Convert.ToDecimal(Textprice.Text);

            Lt.day = txtDay.Text.Trim();

            // Lt.srvtime = "";


            JObject jsonSvtime = new JObject();
            JArray weeksarr = new JArray();

            for (int i = 0; i <= 6; i++)
            {
                var xxCHeck = ((HtmlInputCheckBox)this.Page.FindControl("week" + i));
                if (xxCHeck.Checked == true)
                {
                    weeksarr.Add(new JValue(xxCHeck.Value));

                }
            }


             Lt.out1= out1.Text;
             Lt.out2= out2.Text;
            Lt.out3 = out3.Text;
            Lt.out4 = out4.Text;
           Lt.out5 = out5.Text;
             Lt.out6 = out6.Text;
           Lt.out7 = out7.Text;

            jsonSvtime.Add(new JProperty("weekset", weeksarr));
            var timearr = JArray.Parse(this.timeVue.Text);

            jsonSvtime.Add(new JProperty("times", timearr));
            //  Response.Write(jsonSvtime.ToString());

            Lt.srvtime = jsonSvtime.ToString().Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");


            if (_Id == 0)
            {
                mydb.S_Store_Item.Add(Lt);
                mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");
                // JscriptPrint("自定义标签添加成功啦！", "list.aspx", "Success");
            }
            else
            {
                mydb.SaveChanges();
                Response.Write("<script> var Close=1;</script>");

                // JscriptPrint("自定义标签修改成功啦！", "list.aspx", "Success");
            }
        }
    }

    /// <summary>
    /// 绑定类别DropDownList控制
    /// </summary>
    /// /// <param name="firstItemTxt">第一项显示的文字</param>
    /// <param name="ddl">要绑定的DropDownList控件</param>

    public void SCClassTreeBind(string firstItemTxt, DropDownList dp)
    {
        string tepmValue = "0";
        SCClassTreeBindFV(tepmValue, firstItemTxt, dp);

    }
    public void SCClassTreeBindFV(string firstItemValue, string firstItemTxt, DropDownList dp)
    {
        dp.Items.Clear();
        ListItem list = new ListItem(firstItemTxt, firstItemValue);
        dp.Items.Add(list);

        using (Entities _class = new Entities())
        {
            int a;
            int.TryParse(list.Value, out a);

            var m = from n in _class.S_StoreClass where n.StoreCpareID == a select n;
            foreach (var n in m)
            {
                dp.Items.Add(new ListItem(n.StoreCNmae.ToString(), n.ID.ToString()));
                int leng = 1;
                dpbinder(dp, int.Parse(n.ID.ToString()), leng);
            }
        }
    }



}

